﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InsuranceCompany.Helpers;
using ClientNS = InsuranceCompany.Core.Domain.Client;
using PolicyC = InsuranceCompany.Core.Domain.InsuranceObjects;
using InsuranceEvent = InsuranceCompany.Core.Domain.InsuranceObjects.InsuranceEvent;
using InsuranceRisks = InsuranceCompany.Core.Domain.InsuranceArea.InsuranceRisks;
using InsuranceRisk = InsuranceCompany.Core.Domain.InsuranceArea.InsuranceRisk;
using AccidenP = InsuranceCompany.Core.Domain.InsuranceObjects.Accident;
using Event = InsuranceCompany.Core.Domain.Objects.Event;

namespace InsuranceCompany.Client
{
    public partial class Accident : InsuranceForm
    {
        private ClientNS.Client Client;
        private PolicyC.Policy Policy;
        private AccidenP SelectedAccident;
        
        public Accident()
        {
            InitializeComponent();          
        }
        public Accident(ref List<ObjectsCollection> objects, ClientNS.Client client, PolicyC.Policy policy)
        {
            InitializeComponent();
            this.Objects = objects;
            this.Policy = policy;
            this.Client = client;
            this.AccidentStatusComboBox.SelectedIndex = 0;
            this.UpdateItemsListView();
        }
        private void CloseClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddAccidentButtonClick(object sender, EventArgs e)
        {
            if (this.DescTextBox.Text != "")
            {
                AccidenP accident = new AccidenP();
                accident.Policy = this.Policy;
                accident.Description = this.DescTextBox.Text;
                if (this.LossTextBox.Text == "")
                    this.LossTextBox.Text = "0";
                accident.LossSum = Decimal.Parse(this.LossTextBox.Text);
                accident.AccidentDate = this.dateTimePicker1.Value;
                this.Policy.AddEvent(new InsuranceEvent(accident));
                this.UpdateItemsListView();
            }
        }
        private void UpdateItemsListView()
        {
            this.AccidentsListView.Items.Clear();
            foreach (Event item in this.Policy.GetHistory().GetHistory())
            {
                string eventType = item.GetType().Name;
                if (eventType == "InsuranceEvent")
                {
                    InsuranceEvent ie = (InsuranceEvent)item;
                    if (ie.Document == null)
                        continue;
                    string insuranceDoc = ie.Document.GetType().ToString();
                    if (insuranceDoc == "InsuranceCompany.Core.Domain.InsuranceObjects.Accident")
                    {
                        AccidenP accident = (AccidenP)ie.Document;
                        ListViewItem listViewItem = new ListViewItem();
                        listViewItem.SubItems[0].Text = ie.GetId().ToString();
                        listViewItem.SubItems.Add(accident.AccidentDate.ToShortDateString());
                        listViewItem.SubItems.Add(accident.Description);
                        listViewItem.SubItems.Add(accident.LossSum.ToString());
                        listViewItem.SubItems.Add(accident.CalculatePaymentSum().ToString());
                        listViewItem.SubItems.Add(accident.Result);
                        this.AccidentsListView.Items.Add(listViewItem);
                    }
                }
            }
        }
        private void AccidentStatusComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.SelectedAccident != null)
            {
                this.SelectedAccident.Result = this.AccidentStatusComboBox.SelectedItem.ToString();
                this.UpdateItemsListView();
            }
        }

        private void AccidentsListViewItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (this.AccidentsListView.SelectedItems.Count == 1)
            {
                ListView.SelectedListViewItemCollection selectedItems = this.AccidentsListView.SelectedItems;
                int id = -1;
                foreach (ListViewItem item in selectedItems)
                {
                    id = Int32.Parse(item.SubItems[0].Text);
                    break;
                }
                InsuranceEvent ie = (InsuranceEvent)this.Policy.GetHistory().GetHistory().Find(x => x.GetId() == id);
                AccidenP accident = (AccidenP)ie.Document;
                this.SelectedAccident = accident;
                this.AccidentStatusComboBox.Enabled = true;
            }         
        }
    }
}
